package com.wss.guli.product.service.impl;

import com.wss.guli.product.service.CategoryBrandRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wss.common.utils.PageUtils;
import com.wss.common.utils.Query;

import com.wss.guli.product.dao.BrandDao;
import com.wss.guli.product.entity.BrandEntity;
import com.wss.guli.product.service.BrandService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;


@Service("brandService")
public class BrandServiceImpl extends ServiceImpl<BrandDao, BrandEntity> implements BrandService {
   @Autowired
    CategoryBrandRelationService categoryBrandRelationService;


    /**
     * 分页多条件查询
     * @param params
     * @return
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String key = (String) params.get("key");
        QueryWrapper<BrandEntity> queryWrapper = new QueryWrapper<>();

        if (!StringUtils.isEmpty(key)){
            queryWrapper.eq("brand_id",key).or().like("name",key);
        }



        IPage<BrandEntity> page = this.page(
                new Query<BrandEntity>().getPage(params),queryWrapper

        );

        return new PageUtils(page);
    }

    /**
     * 保持数据表与中间表冗余字段的一致
     * @param brand
     */
    @Transactional
    @Override
    public void updateDetail(BrandEntity brand) {
        //保持冗余字段的一致
        this.updateById(brand);
        if (!StringUtils.isEmpty(brand.getName())){
            //同步更新其他关联表的数据
            categoryBrandRelationService.updateBrand(brand.getBrandId(),brand.getName());
       // TODO 更新其他关联

        }
    }

    @Override
    public List<BrandEntity> getBrandsByIdS(List<Long> brandIds) {

        return baseMapper.selectList(new QueryWrapper<BrandEntity>().eq("brand_id",brandIds));
    }


}